<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>MySQL Connection Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<style type="text/css">
#wrapper {
  width: 600px;
  margin: 20px auto 0;
  font: 1.2em Verdana, Arial, sans-serif;
}
input {
  font-size: 1em;
}
#submit {
  padding: 4px 8px;
}
</style>
</head>

<body>

<div id="wrapper">

<?php
$action = htmlspecialchars($_GET['action'], ENT_QUOTES);
?>

<?php if (!$action) { ?>

  <h1>MySQL connection test</h1>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>?action=test" id="mail" method="post">

  <table cellpadding="2">
    <tr>
      <td></td>
      <td><input type="submit" id="submit" value="Test Connection" tabindex="5" /></td>
      <td></td>
    </tr>
  </table>

</form>

<?php } ?>

<?php if ($action == "test") {

// The variables have not been adequately sanitized to protect against SQL Injection attacks: http://us3.php.net/mysql_real_escape_string
require_once "class.morphini.php";

$Morph = new MorphIni();
$dbhost = $Morph->db_hostname;
$dbport = $Morph->db_port;
$dbtype = $Morph->db_type;
$dbname = $Morph->db_name;
$dbuser = $Morph->db_username;
$dbpass = $Morph->db_password;
$dbconn = "host=$dbhost port=$dbport dbname=$dbname user=$dbuser password=$dbpass";


  $link = mysql_connect($dbhost .':'. $dbport, $dbuser, $dbpass,$dbname);
    if (!$link) {
      echo "<p><b>Could not connect to server:</b> '" . $hostname . "'</p>\n";
          echo mysql_error();
    }else{
      echo "<p><b>Successfully connected to server:</b> '" . $dbhost . "'</p>\n";
      printf("<b>MySQL client info:</b> %s</br>", mysql_get_client_info());
      printf("<b>MySQL host info:</b> %s</br>", mysql_get_host_info());
      printf("<b>MySQL server version:</b> </br>", mysql_get_server_info());
      printf("<b>MySQL protocol version:</b> %s</br>", mysql_get_proto_info());
    }
  if ($link && !$dbname) {
    echo "<p><b>No database name was given. Available databases:</b></p>\n";
    $db_list = mysql_list_dbs($link);
    echo "<pre>\n";
    while ($row = mysql_fetch_array($db_list)) {
        echo $row['Database'] . "\n";
    }
    echo "</pre>\n";
  }
  if ($dbname) {
    $dbcheck = mysql_select_db("$dbname");
    if (!$dbcheck) {
          echo mysql_error();
    }else{
      echo "<p><b>Successfully connected to database:</b> '" . $dbname . "'</p>\n";
      // Check tables
      $sql = "SHOW TABLES FROM `$dbname`";
      $result = mysql_query($sql);
      if (mysql_num_rows($result) > 0) {
        echo "<p><b/>Available tables:</b></p>\n";
        echo "<pre>\n";
        while ($row = mysql_fetch_row($result)) {
          echo "{$row[0]}\n";
        }
        echo "</pre>\n";
        $t=date('l jS \of F Y h:i:s A');
        $sql = "INSERT INTO t1 VALUES(null, '" . $t . "');";
        echo "<p><b>Testing insert:</b> ".$sql."</p>\n";
        $result = mysql_query($sql);
        $sql="SELECT * FROM t1 ORDER BY id DESC LIMIT 1;";
        echo "</p><b>Testing Select:</b> ".$sql."</p>";
        echo "<p><b>Data:</b></p>";
        echo "<pre>\n";
        $result = mysql_query($sql);
        while ($row = mysql_fetch_row($result)) {
          echo "{$row[0]}, $row[1]\n";
        }
        echo "</pre>\n";
      } else {
        echo "<p><b>The database '" . $dbname . "' contains no tables.</b></p>\n";
        $sql= "CREATE TABLE t1 (id MEDIUMINT NOT NULL AUTO_INCREMENT, name CHAR(30) NOT NULL, PRIMARY KEY (id));";
        echo "<p><b>Creating table:</b> ".$sql."</p>";
        $result = mysql_query($sql);
        echo $result;
        echo mysql_error();
      }
?>
</br></br></br>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>?action=test" id="mail" method="post">

  <table cellpadding="2">
    <tr>
      <td></td>
      <td><input type="submit" id="submit" value="ReTest Connection" tabindex="5" /></td>
      <td></td>
    </tr>
  </table>

</form>
</br></br></br>

<?
}
  }
}
?>

</div><!-- end #wrapper -->
<?php phpinfo() ?>
</body>
</html>
